#!/opt/anaconda3/bin/python
# -*- coding:utf-8 -*-
'''
@project : meiduo_mall
@author  : leftxxk
#@file   : detail.py
#@time   : 2021-06-10 18:38:01
'''
import sys

# 让这个脚本到上一级目录运行
sys.path.insert(0, '../')

import os

# 告诉os我们的django的配置文件在哪了
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings")

import django

# 相当于我们有了django
django.setup()

# ---------------------------------以上顺序不可以颠倒-----------------------------------------

from apps.goods.models import SKU
from meiduo_mall import settings
from utils.goods import get_categories, get_goods_specs, get_breadcrumb


def generic_detail_html(sku):
    """商品详情页"""
    # 查询商品频道分类
    categories = get_categories()
    # 查询面包屑导航
    breadcrumb = get_breadcrumb(sku.category)
    # 查询SKU规格信息
    goods_specs = get_goods_specs(sku)
    # 渲染页面
    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs,
    }
    # 1 加载模版
    from django.template import loader
    detail_template = loader.get_template(
        '/Users/leftxxk/PycharmProjects/Django-项目/mei-duo-mall/meiduo_mall/templates/detail.html')
    # 2 模版渲染
    detail_templat_data = detail_template.render(context)
    # 3写入指定文件
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/goods/detail%s.html' % sku.id)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(detail_templat_data)
    print(sku.id)


if __name__ == '__main__':
    skus = SKU.objects.all()
    for sku in skus:
        generic_detail_html(sku)
